<?php
/**
 *
 * User: Jessica@aweb.wang
 * Date: 2023/3/10 14:50
 * Email: <1194323013@qq.com>
 *                            _ooOoo_
 *                           o8888888o
 *                           88" . "88
 *                           (| -_- |)
 *                           O\  =  /O
 *                        ____/`---'\____
 *                      .'  \\|     |//  `.
 *                     /  \\|||  :  |||//  \
 *                    /  _||||| -:- |||||-  \
 *                    |   | \\\  -  /// |   |
 *                    | \_|  ''\---/''  |   |
 *                    \  .-\__  `-`  ___/-. /
 *                  ___`. .'  /--.--\  `. . __
 *               ."" '<  `.___\_<|>_/___.'  >'"".
 *              | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 *              \  \ `-.   \_ __\ /__ _/   .-` /  /
 *         ======`-.____`-.___\_____/___.-`____.-'======
 *                            `=---='
 *        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 *                      Buddha Bless, No Bug !
 **/

namespace app\common\service;
use app\common\exception\BusinessException;
use app\common\exception\BusinessExceptionEnum;
use app\common\model\Administrator;
use app\common\utils\ResponseUtils;
use app\common\utils\TokenUtils;

class AdminService
{
    public function selectLogin($username,$password){
        //查询是否有该用户
        $querywraper=['username'=>$username];
        $administrator=$this->selectByUsername($username);
        //定义返回数组
        $retdata=[];
        if($administrator){
            //对比密码是否正确
            if(md5($password)==(String)$administrator['password']) {
                //生成JWTTOKEN
                $tokenUtils=new TokenUtils();
                $tokenUtils->withClaim("uname",$administrator['username']);
                $tokenUtils->withClaim("uid",$administrator['id']);
                //返回数组中插入Token
                $retdata['token']=$tokenUtils->createToken();
            }
            else{
                throw new BusinessException(BusinessExceptionEnum::PASSWORD_WRONG);
            }
        }else{
            throw new BusinessException(BusinessExceptionEnum::USERNAME_WRONG);
        }
        return $retdata;
    }
    public function selectByUsername($username){
        $where[]=['username','=',$username];
        return Administrator::where($where)->find();
    }
    public function selectList(){
        return Administrator::select();
    }

}